# -*- coding: utf-8 -*-

"""
all functions assume UTF-8 encoding and unicode strings
"""

import codecs
import locale
import types
import chars
from lib.probability import FreqDist, DictionaryConditionalProbDist, DictionaryProbDist
locale.setlocale( locale.LC_CTYPE , 'greek') 
locale.setlocale( locale.LC_COLLATE  , 'greek')

def printSeq(seq, printout=False):
    s=''
    for c in seq:
        s+=c
        s+='-'
    s=s[:-1]
    if printout: print s.encode('utf-8')
    return s

def flatten(x):
    """flatten(sequence) -> list

    Returns a single, flat list which contains all elements retrieved
    from the sequence and all recursively contained sub-sequences
    (iterables).

    Examples:
    >>> [1, 2, [3,4], (5,6)]
    [1, 2, [3, 4], (5, 6)]
    >>> flatten([[[1,2,3], (42,None)], [4,5], [6], 7, MyVector(8,9,10)])
    [1, 2, 3, 42, None, 4, 5, 6, 7, 8, 9, 10]"""

    result = []
    for el in x:
        #if isinstance(el, (list, tuple)):
        if hasattr(el, "__iter__"):
            result.extend(flatten(el))
        else:
            result.append(el)
    return result

def trimList(list):
    res = []
    for l in list:
        if not l is None and l != '':
            res.append(l)
    return res
    
    
def printSeq2(seq):
    """Pretty prints a list of tuples """
    s1=''
    s2=''
    for grkl,gr in seq:
        s1+=grkl
        s1+='-'
        s2+=gr
        s2+='-'        
    s1=s1[:-1]
    s2=s2[:-1]    
    print s1.encode('utf-8'),'/',s2.encode('utf-8')
    return (s1,s2)
        
        

def printCPD(cpd):
        for condition in cpd.conditions():
            print condition.encode('utf-8'), repr(condition)
            print cpd[condition]._prob_dict
            print '---'

def cpd_from_dict(dict, normalize = False):
    
    conditions = {}
    
    for key in dict:
        assert (type(key)==types.TupleType)
        (prev, next) = key
        # initialize the dictionaries as needed
        conditions[prev] = conditions.get(prev, {})
        conditions[prev][next] = dict[key]
    
    for condition in conditions:
        # replace the dictionaries with DictionaryProbDists
        conditions[condition] = DictionaryProbDist(conditions[condition], normalize = normalize)
    
    return DictionaryConditionalProbDist(conditions)
        
        
        
    
    

def cpd(array, conditions, observations):
    d = {} 
    for values, condition in zip(array, conditions):
        d[condition] = pd(values, observations) 
    return DictionaryConditionalProbDist(d)



def pd(values, observations):
     d = {} 
     for value, item in zip(values, observations):
         d[item] = value 
     return DictionaryProbDist(d)


def print_words(words, filename='out.txt'):
    file = open(filename, mode='w')
    file.write(codecs.BOM_UTF8)
    for w in words.sorted_samples():
        file.write(w.encode('utf-8'))
        file.write('-')
        file.write(str(words.count(w)))
        file.write('-')
        file.write('(')
        file.write(str(words.freq(w)))
        file.write(')')
        file.write('\n')
    file.flush
    file.close
    print ' Total %d words, %d unique' % (words.N(), words.B())

import re
def read_words(filename, words=FreqDist()):
    file = open(filename, 'rb')
    contents = file.read() #.decode('utf-8')
    if contents.startswith('\xef\xbb\xbf'):
        contents = contents[3:] #strip BOM       
    contents = unicode(contents, 'utf-8')
    return _read_words(contents, words)

def _read_words(contents, words):
    pattern = re.compile(r'(\w+)-(\d+)-(.+)', re.UNICODE)
    #print repr(contents)
    for line in contents.splitlines():
        #print repr(line)
        #print repr(line.split('-'))
        m = pattern.match(line)
        word = m.group(1)
        count = m.group(2)
        #(word, count, freq) =line.split('-')
        words.inc(word, int(count))
    print ' Total %d words, %d unique' % (words.N(), words.B())
    return words

def refineWords(words):
    "remove non greek words from a FreqDist"
    words2 = FreqDist()
    for w in words.samples():
        if isGreek(w):
            words2.inc(w,words.count(w))
    return words2

def simplifyWords(words): 
    "convert uppercase and accented words into simple, lowercase ones, from a FreqDist"
    words2 = FreqDist()
    for w in words.samples():
        w2 = w.lower().translate(chars.simple_ch)
        words2.inc(w2,words.count(w))
    return words2

def isGreek(word):
    start1=0x0370
    end1=0x03ff
    start2=0x1f00
    end2=0x1fff
    greek_chars =  [c for c in word if ((ord(c)>=start1 and ord(c)<=end1)or(ord(c)>=start2 and ord(c)<=end2))]
    return len(greek_chars)==len(word)

def split_word_bigrams(word):
    parts= []
    buf_chars = []
    for c in word:
        buf_chars.append(c)
        if len(buf_chars)==2:
            parts.append(u''.join(buf_chars))
            buf_chars = []
    if buf_chars:
        parts.append(u''.join(buf_chars))
    return parts


def isGreekCons(char):
    return char in chars.greek_ch_cons;

def isGreekVowel(char):
    return char in chars.greek_ch_vowels;

def isPhoneticCons(char):
    return char in chars.phonetic_grkl_cons

def isVowelCons(char):
    return char in chars.phonetic_grkl_vowels


def isGreeklishCons(char):
    return char in chars.greeklish_ch_cons;

def isGreeklishVowel(char):
    return char in chars.greeklish_ch_vowels;

        

import os, fnmatch
def all_files(root, patterns='*', single_level=False, yield_folders=False):
    # Expand patterns from semicolon-separated string to list
    patterns = patterns.split(';')
    for path, subdirs, files in os.walk(root):
        if yield_folders:
            files.extend(subdirs)
        files.sort( )
        for name in files:
            for pattern in patterns:
                if fnmatch.fnmatch(name, pattern):
                    yield os.path.join(path, name)
                    break
        if single_level:
            break

def test_cons_vowels():
    print isGreekCons(u'α')
    print isGreekCons(u'β')
    print isGreekCons(u'σ')
    print isGreekVowel(u'σ')
    print isGreekVowel(u'α')
    print isGreekVowel(u'σ')
    print isGreeklishCons('a')
    print isGreeklishCons('b')
    print isGreeklishCons('d')
    print isGreeklishVowel('d')
    print isGreeklishVowel('a')
    print isGreeklishVowel('r')    
    
if __name__ == '__main__':
    test_cons_vowels()

        

